package com.wing.order.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.enterprise.feign.EnterpriseClient;
import com.wing.enterprise.product.model.response.ProductDetailVO;
import com.wing.enterprise.product.model.response.ProductUnitVO;
import com.wing.enterprise.product.model.response.ProductVO;
import com.wing.order.model.entity.InventoryOutPlan;
import com.wing.order.model.response.InventoryOutPlanDetailVO;
import com.wing.order.model.response.InventoryOutPlanVO;
import com.wing.order.service.InventoryOutPlanDetailService;
import com.wing.order.service.InventoryOutPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-云仓入库计划单列表", tags = "#/order/cloudStoragePlan")
@RestController("cloudStoragePlanController")
@RequestMapping("order/admin/cloudStoragePlan")
@RequiredArgsConstructor
public class CloudStoragePlanController {

    private final InventoryOutPlanService inventoryOutPlanService;
    private final InventoryOutPlanDetailService inventoryOutPlanDetailService;
    private final EnterpriseClient enterpriseClient;

    @ApiOperation(value = "查询所有云仓入库计划单", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<InventoryOutPlan> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,InventoryOutPlan inventoryOutPlan
    ){

        //搜索
        inventoryOutPlan.setInoutTypeCode("allotCloudWarehouse");
        IPage<InventoryOutPlan> list = inventoryOutPlanService.getInventoryOutPlanPageList(inventoryOutPlan,page,limit);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }
    @GetMapping("/getProductDetailList")
    public JsonResult getProductDetailList(@RequestParam(value = "productSkuCodeArr[]") String[] productSkuCodeArr){
        List<ProductDetailVO> list = new ArrayList<ProductDetailVO>();
        for (String item : productSkuCodeArr){
            JsonResult<ProductDetailVO> productDetailJson = enterpriseClient.getProductDetailBySkuCode(item);
            JsonResult<ProductVO> productJson = enterpriseClient.getProductById(productDetailJson.getData().getProductId());
            productDetailJson.getData().setProductName(productJson.getData().getName());
            JsonResult<ProductUnitVO> unitJson = enterpriseClient.getProductUnitById(productJson.getData().getUnitId());
            productDetailJson.getData().setUnitId(unitJson.getData().getId());
            productDetailJson.getData().setUnitName(unitJson.getData().getName());
            list.add(productDetailJson.getData());
        }
        return JsonResult.success(list);
    }
    @GetMapping("/getInventoryOutPlanById")
    public JsonResult getInventoryOutPlanById(@RequestParam(value = "id") Long id){
        InventoryOutPlanVO info = inventoryOutPlanService.getInventoryOutPlanById(id);
        return JsonResult.success(info);
    }
    @ApiOperation(value = "查询出库计划商品信息", notes = "查询")
    @GetMapping("/getInventoryOutPlanDetailVOList")
    public JsonResult getInventoryOutPlanDetailVOList(@RequestParam(value = "planId") Long planId) {
        List<InventoryOutPlanDetailVO> list = inventoryOutPlanDetailService.getInventoryOutPlanDetailVOList(planId);
        return JsonResult.success(list);
    }
    @ApiOperation(value = "审核出库单计划", notes = "修改")
    @PutMapping("/updateStatus")
    public JsonResult updateStatus(@RequestParam(value = "id") Long id) {
        InventoryOutPlan inventoryOutPlan = inventoryOutPlanService.getById(id);
        if(inventoryOutPlan == null){
            return JsonResult.fail("修改失败！");
        }
        inventoryOutPlan.setStatus(InventoryOutPlan.Status.confirm);
        if (inventoryOutPlanService.updateById(inventoryOutPlan)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }


}
